using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;

/// <summary>
/// AKS 2.0 - HiSoftware Inc.
/// SA DESIGN:
/// Robert B. Yonaitis 
/// </summary>

/// <variables>
/// oOutput         -   An object used in outputting the finalized code
/// sOutputBuilder  -   A string containing the code to be rendered to the page
/// oWriter         -   An object used to output the rendered code
/// sSearchString   -   A string used to store the string being used to locate the correct control to alter
/// iStart          -   An integer which stores the starting index of the sSearchString when located
/// iSpacing        -   An integer which stores the length of sSearchString and is used for placing text after sSearchString
/// </variables>

namespace AKSAdapters
{
	public class AKS_ChoiceFilterTextbox_SmartAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter
	{
		public AKS_ChoiceFilterTextbox_SmartAdapter()
		{

		}

		protected override void Render(HtmlTextWriter oOutput)
		{
			//Initializes sOutputBuilder as a new StringBuilder object
			StringBuilder sOutputBuilder = new StringBuilder();
			HtmlTextWriter oWriter = new HtmlTextWriter(new StringWriter(sOutputBuilder));
			base.Render(oWriter);

			//Initialize sSearchString
			string sSearchString = null;

			//Assign sSearchString to the string used to identify the correct control
			sSearchString = "Picker_SelectionBox\" title=\"Selected values\"";

			//Initialize iStart as the starting index of sSearchString
			int iStart = sOutputBuilder.ToString().IndexOf(sSearchString);

			//If iStart is greater than 0, sSearchString has been found and the adapter will proceed with the alteration(s)
			//Otherwise, the adapter will not change anything and proceeds to output.
			if (iStart > 0)
			{
				//Initialize iSpacing as the length of sSearchString
				int iSpacing = sSearchString.Length;

				//Inserts class="ms-choicefiltertxtbx" after sSearchString
				sOutputBuilder.Insert((iStart + iSpacing), " class=\"ms-choicefiltertxtbx\" ");
			}

			//Output sOutputBuilder to be rendered
			oOutput.Write(sOutputBuilder.ToString());
		}
	}
}
